//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package requirement_dao

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/project_requirement_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type KanoRowDbItem struct {
	Like     uint32 `bson:"like"`
	Expect   uint32 `bson:"expect"`
	Neutral  uint32 `bson:"neutral"`
	Tolerate uint32 `bson:"tolerate"`
	Dislike  uint32 `bson:"dislike"`
}

func ConvertKanoRow(row *project_requirement_api.KanoRow) KanoRowDbItem {
	return KanoRowDbItem{
		Like:     row.Like,
		Expect:   row.Expect,
		Neutral:  row.Neutral,
		Tolerate: row.Tolerate,
		Dislike:  row.Dislike,
	}
}

func (item *KanoRowDbItem) ToKanoRow() *project_requirement_api.KanoRow {
	return &project_requirement_api.KanoRow{
		Like:     item.Like,
		Expect:   item.Expect,
		Neutral:  item.Neutral,
		Tolerate: item.Tolerate,
		Dislike:  item.Dislike,
	}
}

type KanoInfoDbItem struct {
	RequirementId string        `bson:"_id"`
	LikeVsRow     KanoRowDbItem `bson:"likeVsRow"`
	ExpectVsRow   KanoRowDbItem `bson:"expectVsRow"`
	NeutralVsRow  KanoRowDbItem `bson:"neutralVsRow"`
	TolerateVsRow KanoRowDbItem `bson:"tolerateVsRow"`
	DislikeVsRow  KanoRowDbItem `bson:"dislikeVsRow"`
}

func (item *KanoInfoDbItem) ToKanoInfo() *project_requirement_api.KanoInfo {
	return &project_requirement_api.KanoInfo{
		RequirementId: item.RequirementId,
		LikeVsRow:     item.LikeVsRow.ToKanoRow(),
		ExpectVsRow:   item.ExpectVsRow.ToKanoRow(),
		NeutralVsRow:  item.NeutralVsRow.ToKanoRow(),
		TolerateVsRow: item.TolerateVsRow.ToKanoRow(),
		DislikeVsRow:  item.DislikeVsRow.ToKanoRow(),
	}
}

type _KanoInfoDao struct {
	mc *mongo.Collection
}

func newKanoInfoDao(db *mongo.Database) (*_KanoInfoDao, error) {
	mc := db.Collection(db_ns.DB_NS_PROJECT_REQUIRE_MENT + "kano_info")
	return &_KanoInfoDao{mc: mc}, nil
}

func (db *_KanoInfoDao) Set(ctx context.Context, dbItem *KanoInfoDbItem) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": dbItem.RequirementId}, bson.M{"$set": dbItem},
		options.Update().SetUpsert(true))
	return err
}

func (db *_KanoInfoDao) Get(ctx context.Context, requirementId string) (*KanoInfoDbItem, error) {
	retItem := &KanoInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": requirementId}).Decode(retItem)
	return retItem, err
}

func (db *_KanoInfoDao) Remove(ctx context.Context, requirementId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": requirementId})
	return err
}
